--  ---------------------------------------------------------------------------
--  @File: symbol_table.adb
--
--  @Project: LiberAda
--  @Version: 1.0
--  @Created: 2009-11-03
--  @Author:  Ivan Bogicevic
--
--  @Description:
--    For a detailed description of this package (@see symbol_table.ads).
--  @Implementation:
--    A list of symbols, including their html linkage destination, their
--    context (which packages are currently used) and references to their
--    hmtl parts.
--
--  @Modifications:
--
--  ---------------------------------------------------------------------------
with Ada.Text_IO;

with Settings;

package body Symbol_Table is

   --  ------------------------------------------------------------------------
   --  @Procedure: Add_Symbol
   procedure Add_Symbol (Symb : Symbol.Symbol_Type) is
      New_Entry : Entry_Type;
   begin
      New_Entry.Symb := Symb;
      New_Entry.Link := Symb.Get_Link;
      Entries.Append (New_Entry);
   end Add_Symbol;

   --  ------------------------------------------------------------------------
   --  @Procedure: Add_Link
   procedure Add_Link (Link : ASU.Unbounded_String) is
      --  New_Entry : Entry_Type;
   begin
      --  TODO: Add table-entry without a symbol (only link, context and parts)
      null;
   end Add_Link;

   --  ------------------------------------------------------------------------
   --  @Procedure: Resolve
   procedure Resolve is
      Table_Cursor    : Symbol_Table.Entries_Type.Cursor;
      Current_Entry   : Symbol_Table.Entry_Type;
      Current_Symbol  : Symbol.Symbol_Type;
      Current_Link    : ASU.Unbounded_String;
      Current_Context : String_Vector.Vector;
      Context_Cursor  : String_Vector.Cursor;
   begin
      --  Output symbol table
      if Settings.Verbose then
         Ada.Text_IO.New_Line;
         Ada.Text_IO.New_Line;
         Ada.Text_IO.Put ("--- SYMBOL TABLE: ---------------------------");
         Ada.Text_IO.Put_Line ("---------------------------------------");
         Table_Cursor := Symbol_Table.Entries.First;
         while Symbol_Table.Entries_Type.Has_Element (Table_Cursor) loop
            --  Get the table entry
            Current_Entry := Symbol_Table.Entries_Type.Element (Table_Cursor);
            --  Get the values of the table entry
            Current_Symbol := Current_Entry.Symb;
            Current_Link   := Current_Entry.Link;
            Current_Context := Current_Entry.Context;
            --  Output values to console
            Ada.Text_IO.New_Line;
            Ada.Text_IO.Put_Line ("Symbol:  " & ASU.To_String (Symbol.Get_ID (
              Current_Symbol)));
            Ada.Text_IO.Put_Line ("Link:    " & ASU.To_String (Current_Link));
            Ada.Text_IO.Put ("Context: ");
            Context_Cursor := Current_Context.First;
            while String_Vector.Has_Element (Context_Cursor) loop
               Ada.Text_IO.Put (ASU.To_String (
                 String_Vector.Element (Context_Cursor)));
               Context_Cursor := String_Vector.Next (Context_Cursor);
            end loop;
            Ada.Text_IO.New_Line;
            Ada.Text_IO.Put ("#Parts: ");
            Ada.Text_IO.Put (Parts_Vector.Length (Current_Entry.Parts)'Img);
            Ada.Text_IO.New_Line;
            --  Go to next symbol
            Table_Cursor := Symbol_Table.Entries_Type.Next (Table_Cursor);
         end loop;
      end if;
      --  TODO: Get all unresolved symbols of the table
      --  TODO: Resolve them
   end Resolve;

   --  ------------------------------------------------------------------------
   --  @Function: "="
   function "=" (Left, Right : Entry_Type) return Boolean is
      use Symbol;
   begin
      return Left.Symb = Right.Symb;
   end "=";

end Symbol_Table;
